<?php
require_once 'common/config.php';
require_once 'common/functions.php';
// 检查登录状态
check_login();
$db = db_connect();
// 获取搜索和筛选参数
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$status = isset($_GET['status']) ? intval($_GET['status']) : -1;
// 每页显示的记录数
$per_page = 10;
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$offset = ($page - 1) * $per_page;
// 构建查询条件
$where = "1=1";
$params = [];
if ($search) {
    $where .= " AND (name LIKE :search OR phone LIKE :search OR description LIKE :search)";
    $params[':search'] = "%{$search}%";
}
if ($status != -1) {
    $where .= " AND status = :status";
    $params[':status'] = $status;
}
// 获取总记录数
$count_sql = "SELECT COUNT(*) FROM coaches WHERE {$where}";
$count_stmt = $db->prepare($count_sql);
foreach ($params as $key => $value) {
    $count_stmt->bindValue($key, $value);
}
$count_stmt->execute();
$total_records = $count_stmt->fetchColumn();
$total_pages = ceil($total_records / $per_page);
// 获取教练列表
$sql = "SELECT * FROM coaches 
        WHERE {$where} 
        ORDER BY created_at DESC
        LIMIT {$offset}, {$per_page}";

$stmt = $db->prepare($sql);
foreach ($params as $key => $value) {
    $stmt->bindValue($key, $value);
}
$stmt->execute();
$coaches = $stmt->fetchAll();
// 状态映射
$status_types = [
    0 => ['name' => '禁用', 'badge' => 'badge-danger'],
    1 => ['name' => '正常', 'badge' => 'badge-success']
];
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>教练管理 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
</head>
<body>
    <div class="wrapper">
        <?php include 'includes/sidebar.php'; ?>
        
        <div class="main-content">
            <header class="top-header">
                <div class="header-left">
                    <h2>教练管理</h2>
                </div>
                <div class="header-right">
                    <a href="coach_add.php" class="btn btn-primary">
                        <i class="fas fa-plus"></i> 添加教练
                    </a>
                </div>
            </header>

            <div class="content">
                <!-- 搜索和筛选 -->
                <div class="filter-bar">
                    <form action="" method="GET" id="searchForm" class="search-bar">
                        <div class="search-input">
                            <input type="text" name="search" placeholder="搜索教练姓名、手机号..." 
                                   value="<?php echo htmlspecialchars($search); ?>">
                        </div>
                        <div class="status-select">
                            <select name="status" id="status-select" onchange="document.getElementById('searchForm').submit()">
                                <option value="-1">全部状态</option>
                                <option value="1" <?php echo $status === 1 ? 'selected' : ''; ?>>启用</option>
                                <option value="0" <?php echo $status === 0 ? 'selected' : ''; ?>>禁用</option>
                            </select>
                        </div>
                        <button type="submit" class="btn-search">
                            <i class="fas fa-search"></i>
                        </button>
                    </form>
                </div>

                <!-- 数据表格 -->
                <div class="data-table">
                    <table>
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>头像</th>
                                <th>姓名</th>
                                <th>手机号</th>
                                <th>简介</th>
                                <th>状态</th>
                                <th>添加时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($coaches as $coach): ?>
                            <tr>
                                <td><?php echo $coach['id']; ?></td>
                                <td>
                                    <?php if ($coach['avatar']): ?>
                                    <img src="../api/user/<?php echo htmlspecialchars($coach['avatar']); ?>" 
                                         class="user-avatar" 
                                         onclick="showImage(this.src)"
                                         alt="教练头像">
                                    <?php endif; ?>
                                </td>
                                <td><?php echo htmlspecialchars($coach['name']); ?></td>
                                <td><?php echo htmlspecialchars($coach['phone']); ?></td>
                                <td class="description-cell"><?php echo htmlspecialchars($coach['description']); ?></td>
                                <td>
                                    <span class="badge <?php echo $status_types[$coach['status']]['badge']; ?>">
                                        <?php echo $status_types[$coach['status']]['name']; ?>
                                    </span>
                                </td>
                                <td><?php echo date('Y-m-d H:i', strtotime($coach['created_at'])); ?></td>
                                <td>
                                    <a href="coach_edit.php?id=<?php echo $coach['id']; ?>" 
                                       class="btn-icon" title="编辑">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <?php if ($coach['status'] == 1): ?>
                                    <a href="javascript:void(0)" 
                                       onclick="changeStatus(<?php echo $coach['id']; ?>, 0)"
                                       class="btn-icon text-warning" title="禁用">
                                        <i class="fas fa-ban"></i>
                                    </a>
                                    <?php else: ?>
                                    <a href="javascript:void(0)" 
                                       onclick="changeStatus(<?php echo $coach['id']; ?>, 1)"
                                       class="btn-icon text-success" title="启用">
                                        <i class="fas fa-check-circle"></i>
                                    </a>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <!-- 添加分页 -->
                <?php 
                echo generate_pagination($page, $total_pages, [
                    'search' => $search,
                    'status' => $status
                ]); 
                ?>
            </div>
        </div>
    </div>

    <!-- 图片预览模态框 -->
    <div id="imageModal" class="modal" onclick="this.style.display='none'">
        <img id="modalImage" class="modal-content">
    </div>

    <script>
    function showImage(src) {
        const modal = document.getElementById('imageModal');
        const modalImg = document.getElementById('modalImage');
        modal.style.display = "block";
        modalImg.src = src;
    }

    function changeStatus(id, status) {
        const action = status ? '启用' : '禁用';
        if (confirm(`确定要${action}这个教练吗？`)) {
            window.location.href = `coach_status.php?id=${id}&status=${status}`;
        }
    }

    // 状态选择改变时自动提交表单
    document.getElementById('status-select').addEventListener('change', function() {
        document.getElementById('searchForm').submit();
    });
    </script>

    <style>
    .description-cell {
        max-width: 300px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .filter-bar {
        background: #fff;
        padding: 10px;
        border-radius: 4px;
        box-shadow: 0 1px 3px rgba(0,0,0,0.05);
        margin-bottom: 20px;
    }

    .search-bar {
        display: flex;
        margin-top: 12px;
        align-items: center;
        gap: 8px;
    }

    .search-input {
        flex: 1;
    }

    .search-input input {
        width: 100%;
        padding: 6px 12px;
        border: 1px solid #ddd;
        border-radius: 4px;
        font-size: 14px;
        line-height: 1.5;
        background: #f8f9fa;
        transition: all 0.3s;
    }

    .search-input input:focus {
        border-color: #3498db;
        background: #fff;
        outline: none;
    }

    .status-select select {
        padding: 6px 12px;
        border: 1px solid #ddd;
        border-radius: 4px;
        font-size: 14px;
        line-height: 1.5;
        background: #f8f9fa;
        min-width: 100px;
        cursor: pointer;
        transition: all 0.3s;
    }

    .status-select select:focus {
        border-color: #3498db;
        background: #fff;
        outline: none;
    }

    .btn-search {
        padding: 6px 16px;
        background: #3498db;
        color: #fff;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        line-height: 1.5;
        transition: background 0.3s;
    }

    .btn-search:hover {
        background: #2980b9;
    }

    /* 响应式布局 */
    @media (max-width: 768px) {
        .search-bar {
            flex-direction: column;
        }
        
        .search-input,
        .status-select,
        .btn-search {
            width: 100%;
        }
        
        .status-select select {
            width: 100%;
        }
    }
    </style>
</body>
</html> 